- Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathReverseStack.java
37 lines (27 loc) Β· 800 Bytes
/
ReverseStack.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
packagesection10_Stacks;
publicclassReverseStack {
publicstaticvoidmain(String[] args) throwsException {
StackUsingArraystack1 = newStackUsingArray(5);
for (inti = 10; i <= 50; i += 10) {
stack1.push(i);
}
stack1.display();
StackUsingArrayhelperStack = newStackUsingArray(5);
intcurrentDataPos = 0;
reverseStack(stack1, helperStack, currentDataPos);
System.out.println("\n after reversing stack...\n");
stack1.display();
}
staticvoidreverseStack(StackUsingArraystack1, StackUsingArrayhelper, intid) throwsException {
if (stack1.isEmpty())
return;
intcurrentItem = stack1.pop();
reverseStack(stack1, helper, id + 1);
helper.push(currentItem);
if (id == 0) {
while (!helper.isEmpty()) {
stack1.push(helper.pop());
}
}
}
}